var mysql = require('mysql');

// 创建一个connection  
var connection = mysql.createConnection({      
    host: 'localhost',        
    user: 'root',                
    password: 'admin',         
    database: 'student',  
    port: '3306'                  
});

// 创建一个connection  
connection.connect(function(err){  
    if(err){         
        console.log('[query] - :'+err);  
        return;  
    }  
    console.log('[connection connect]  succeed!');  
}); 

// 插入数据
var userAddSql = 'insert into user (uname, pwd) values(?,?)';
var param = ['ccc', 'aaa'];
connection.query(userAddSql, param, function(err, rs){
	if(err){
		console.log('insert err:', err.message);
		return;
	}
	console.log('insert success');
});

// 查询数据
connection.query('SELECT * from user', function(err, rs, fields){
	if(err) {
		console.log('[query]-:' + err);
		return;
	}
	console.log('The solution is:', rs[0].uname);
	// console.log(fields);
}); 

// 关闭connection
connection.end(function(err){
	if(err){
		return;
	}
	console.log('[connection end] succeed!');
});